#include "ctreeitem.h"

#include <QByteArray>
#include <QPixmap>
#include <QStringList>

#include <QtCrypto>

CTreeItem::CTreeItem(CDatabase &db, QString id, CTreeItem *parent):
        m_db(db), m_id(id), m_parent(parent)
{

}

CTreeItem::~CTreeItem()
{
    qDeleteAll(m_items);
}

void CTreeItem::appendChild(CTreeItem *item)
{
    m_items.append(item);
}

CTreeItem *CTreeItem::child(int row)
{
    return m_items.value(row);
}

int CTreeItem::childCount() const
{
    return m_items.count();
}

CTreeItem *CTreeItem::parent()
{
    return m_parent;
}

int CTreeItem::row() const
{
    if (m_parent)
        return m_parent->m_items.indexOf(const_cast<CTreeItem*>(this));
     return 0;
}

QVariant CTreeItem::icon()
{
    if(!getData())
        return QVariant();

    //debug("Type: " + QString::number(m_data.value("icon").type()));

/*    if(m_data.value("icon").isValid())
        debug("Valid icon");
    else
        debug("Not valid icon");*/

    return m_data.value("icon");
}

QVariant CTreeItem::name()
{
    if(!getData())
        return QVariant();

    return m_data.value("name");
}

QString CTreeItem::id()
{
    return m_id;
}

int CTreeItem::entries()
{
    if(!getData())
        return 0;

    return m_data.value("entries").toInt();
}

bool CTreeItem::getData()
{
    if(m_data.count())
        return true;

    m_data = m_db.getSection(m_id);

    if(m_data.contains("icon"))
    {
        QString id = "icon";
        QByteArray buffer = m_data.value(id).toByteArray();

        m_data.remove(id);

        QPixmap pix;
        pix.loadFromData(buffer);
        if(!pix.isNull())
        {
            m_data.remove(id);
            m_data.insert(id, pix);
        }
        else
            if(m_data.contains(id + "_id"))
            {
                QPixmap pix(":/icons/" + m_data.value(id + "_id").toString());
                if(!pix.isNull())
                {
                    m_data.insert(id, pix);
                    m_icon_id = m_data.value(id + "_id").toString();
                }
            }

        if(m_data.value(id).isNull())
            m_data.insert(id, QPixmap(":/icons/subsection_default"));
    }

    if(m_data.contains("name"))
    {
        QCA::SecureArray arr = CDatabase::decrypt_array(m_data.value("name").toByteArray());
        m_data.remove("name");

        QByteArray name = arr.toByteArray();
        m_data.insert("name", QString::fromUtf8(name.data()));
    }

    return m_data.count();
}

QString CTreeItem::getStringById(int id)
{
    switch(id)
    {
        case Name: return "name";
        case Icon: return "icon";
        case IconId: return "icon_id";
        case Id: return "id";
        case Parent: return "parent";
    }

    return QString();
}

void CTreeItem::clear()
{
    m_data.clear();
}

QString CTreeItem::icon_id()
{
    return m_icon_id;
}

void CTreeItem::deleteChild(CTreeItem *item)
{
    int count = m_items.count();
    for(int i=0; i<count; i++)
        if(m_items.at(i)->id()==item->id())
        {
            m_items.removeAt(i);
            break;
        }
}

CTreeItem* CTreeItem::findById(QString id)
{
    if(m_id==id)
        return this;

    foreach(CTreeItem *item, m_items)
    {
        CTreeItem *result = item->findById(id);
        if(result)
            return result;
    }

    return 0;
}

int CTreeItem::findChildRow(QString id)
{
    int count = m_items.count();
    for(int i=0; i<count; i++)
        if(m_items.at(i)->id()==id)
            return i;

    return -1;
}
